#include <stdio.h>
#include <limits.h>

int bsearch (int *arr, int lowIndex, int highIndex, int val) //lowIndex and highIndex that is an array range
{
	if (lowIndex > highIndex) return INT_MIN; // if the value is absent or wrong parameters of the function return INT_MIN 

	if (*(arr + lowIndex) >= val) return INT_MIN; 
	if (*(arr + highIndex) <= val) return INT_MIN; 

	int mid = (highIndex + lowIndex) / 2;
	
	if (*(arr + mid) < val) return bsearch(arr, mid, highIndex, val);
	if (*(arr + mid) > val) return bsearch(arr, lowIndex, mid, val);
		else return *(arr + mid + 1);
}

int main(void) {
	int testArr[9] = {-1,1,3,4,8,12,44,81,99};
	int result = 81;
	printf("%d", bsearch(testArr, 0, 8, result));
	system("pause");
	return 0;
}
